package com.gxa.saas.portal.jms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.saas.core.entity.vo.luoj.OrderCartVO;
import com.gxa.saas.core.entity.vo.luoj.SecKillInfoVO;
import com.gxa.saas.core.entity.vo.luoj.SecKillOrderVO;
import com.gxa.saas.portal.mapper.OrderCartMapper;
import com.gxa.saas.portal.mapper.SecKillMapper;
import com.gxa.saas.portal.service.impl.SeckillServiceImpl;
import com.gxa.saas.portal.thread.UserThreadLocal;
import com.gxa.saas.portal.util.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.jms.Message;
import java.time.LocalDateTime;

@Component
@Slf4j
public class SecKillOrderListener {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private OrderCartMapper orderCartMapper;

    @Resource
    private SecKillMapper secKillMapper;

    private static final String ORDER_USER_ID = "orderUserId";

    @JmsListener(destination = "seckillOrder",containerFactory = "containerFactory")
    public void doOrder(Message message) throws Exception {
        String text = ((ActiveMQTextMessage) message).getText();
        ObjectMapper objectMapper = new ObjectMapper();
        SecKillInfoVO secKillVO = objectMapper.readValue(text, SecKillInfoVO.class);
        try {
            String jmsMessageID = message.getJMSMessageID();
            log.info("-----------{}-------",jmsMessageID);
            //利用redis判断是否存在该消息
            HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
            Object o = hashOperations.get(ORDER_USER_ID, jmsMessageID);
            //如果返回结果不为空，说明该消息id曾经被记录过->是一条被成功消费过的消息
            if(!StringUtils.isEmpty(o)){
                message.acknowledge();
                return ;
            }
            SecKillOrderVO secKillOrderVO = secKillMapper.selectInfo(secKillVO.getGoodsId());
            System.out.println(secKillOrderVO);
            OrderCartVO orderCartVO = new OrderCartVO();
            String uuid = Uuid.get16UUID();
            orderCartVO.setOdId(Integer.valueOf(uuid));
            orderCartVO.setUserId(secKillVO.getUserId());
            orderCartVO.setOdStatus(0);
            orderCartVO.setComName(secKillOrderVO.getPrName());
            orderCartVO.setOdNumber(Long.valueOf(uuid));
            orderCartVO.setOdTime(LocalDateTime.now());
            orderCartVO.setOdPrice(secKillOrderVO.getSeckillPrice());
            orderCartVO.setComNum(1);
            orderCartVO.setMStatus(0);
//            orderCartVO.setShAddress(secKillOrderVO.getAdAddress()+secKillOrderVO.getAdDetail());
//            orderCartVO.setShName(secKillOrderVO.getAdName());
//            orderCartVO.setShPhone(Long.valueOf(secKillOrderVO.getAdPhone()));
            int insert = orderCartMapper.insert(orderCartVO);
            if (insert>0){
                System.out.println("插入成功！");
            }else{
                System.out.println("插入失败！");
            }
            hashOperations.put(ORDER_USER_ID,jmsMessageID,1);
            message.acknowledge();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
